using MediatR;
using UniversalAdmin.Application.Dtos.Log;
using UniversalAdmin.Application.Services.Interfaces;
using UniversalAdmin.Domain.Entities.App;
using System.Threading;
using System.Threading.Tasks;

namespace UniversalAdmin.Application.Services.Handlers;

public class UserQueriedEventHandler : INotificationHandler<UserQueriedEvent>
{
    private readonly ILogService _logService;
    private readonly IUserContextService _userContextService;
    
    public UserQueriedEventHandler(ILogService logService, IUserContextService userContextService)
    {
        _logService = logService;
        _userContextService = userContextService;
    }
    
    public async Task Handle(UserQueriedEvent notification, CancellationToken cancellationToken)
    {
        Console.WriteLine($"UserQueriedEventHandler: {notification.User.Id}");

        var log = new CreateLogDto
        {
            UserId = _userContextService.GetCurrentUserId() ?? notification.User.Id, // 使用当前操作者ID
            Action = "Query",
            Resource = notification.User.Username,
            ResourceId = notification.User.Id
        };
        await _logService.AddAsync(log);
        // 可扩展：如写控制台日志
        System.Console.WriteLine($"用户被查询，用户名：{notification.User.Username}");
    }
}